package com.atguigu.eduservice.controller;


import com.atguigu.common.R;
import com.atguigu.common.cron.bean.FrontPageBean;
import com.atguigu.common.cron.bean.PageListBean;
import com.atguigu.common.cron.vo.ResCollectVo;
import com.atguigu.eduservice.entity.EduCourseCollect;
import com.atguigu.eduservice.service.EduCourseCollectService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程收藏 前端控制器
 * </p>
 *
 * @author Panghl
 * @since 2021-03-16
 */
@RestController
@RequestMapping("/eduservice/edu-course-collect")
public class EduCourseCollectController {
    @Autowired
    private EduCourseCollectService collectService;

    @ApiOperation(value = "收藏")
    @PostMapping("/hitCollect")
    public R hitCollect(@RequestBody(required = false) EduCourseCollect collect) {
        try {
            if (!StringUtils.isEmpty(collect)) {
                collectService.hitCollect(collect);
                return R.ok();
            }
            return R.error().data("没有必要值！");
        } catch (Exception e) {
            return R.error();
        }
    }

    @ApiOperation(value = "取消收藏")
    @DeleteMapping("/cancelCollect/{courseId}/{memberId}")
    public R cancelCollect(@PathVariable("courseId") String courseId,
                           @PathVariable("memberId") String memberId) {
        try {
            collectService.cancelCollect(courseId, memberId);
            return R.ok();
        } catch (Exception e) {
            return R.error();
        }
    }

    @ApiOperation(value = "是否收藏")
    @GetMapping("/isCollect/{courseId}/{memberId}")
    public R isCollect(@PathVariable("courseId") String courseId,
                       @PathVariable("memberId") String memberId) {
        try {
            boolean isCollect = collectService.isCollect(courseId, memberId);
            return R.ok().data("isCollect",isCollect);
        } catch (Exception e) {
            return R.error();
        }
    }

    @ApiOperation(value = "收藏列表")
    @GetMapping("/getPageCollect/{pageNo}/{pageSize}")
    public R getPageCollect(@PathVariable("pageNo") Integer pageNo,
                           @PathVariable("pageSize") Integer pageSize,
                           @RequestParam(required = true,value = "memberId")String memberId) {
        try {
            PageListBean pageListBean= collectService.queryPage(pageNo,pageSize,memberId);
            return R.ok().data(pageListBean);
        } catch (Exception e) {
            return R.error();
        }
    }
}
